package org.springblade.modules.admin.controller.ai;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.ai.RobotCreatePlanDTO;
import org.springblade.modules.admin.entity.ElevatorDetail;
import org.springblade.modules.admin.entity.MaintainMalfunctionRescue;
import org.springblade.modules.admin.entity.MaintainPlanContent;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.service.ElevatorService;
import org.springblade.modules.admin.service.IChatGroupBuildingService;
import org.springblade.modules.admin.service.IElevatorDetailService;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springblade.modules.admin.vo.ai.RobotCreatePlanVO;
import org.springblade.modules.admin.vo.ai.RobotPlanDetailVO;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 机器人聊天数据控制器
 * @author: Sharry
 * @createTime: 2025-05-29 15:21
 * @version: Version-1.0
 */
@Slf4j
@Component
@ResponseBody
@RequiredArgsConstructor
@RequestMapping("/robot/chat")
public class RobotChatController {

	@Resource
	private IChatGroupBuildingService chatGroupBuildingService;

	@Resource
	private IElevatorDetailService elevatorDetailService;

	/**
	 * 根据群聊id获取当前项目的梯号列表
	 */
	@GetMapping("/elevator/address/by/roomId")
	@ApiOperation("根据群id获取当前项目梯号列表")
	@ApiOperationSupport(order = 1)
	public R<List<String>> getElevatorAddressListByChatroomId(String roomId){
		return R.data(chatGroupBuildingService.getElevatorAddressListByChatroomId(roomId));
	}

	/**
	 * 根据群聊id以及梯号创建维修或救援工单
	 */
	@PostMapping("/create/work/plan")
	@ApiOperation("根据群聊id以及梯号创建维修或救援工单")
	@ApiOperationSupport(order = 2)
	public R<Long> createWorkPlan(@RequestBody @Valid RobotCreatePlanDTO robotCreatePlanDTO) {
		// 参数校验
		List<Integer> typeList = new ArrayList<>(2);
		typeList.add(MaintainPlanContentEnum.MAINTAIN_WX.getId());
		typeList.add(MaintainPlanContentEnum.MAINTAIN_KR.getId());
		if(!typeList.contains(robotCreatePlanDTO.getType())){
			log.warn("工单类型不在机器人报障范围内");
			return null;
		}
		// 根据 roomId 查询对应的电梯id
		List<RobotCreatePlanVO> elevatorList = chatGroupBuildingService.getElevatorListByChatroomId(robotCreatePlanDTO);
		if(Func.isEmpty(elevatorList)){
			log.warn("当前群聊没有电梯信息");
			return null;
		}
		RobotCreatePlanVO robotCreatePlanVO = elevatorList.get(0);
		if(new Integer(MaintainPlanContentEnum.MAINTAIN_WX.getId()).equals(robotCreatePlanDTO.getType())){
			// 组装条件并创建维修工单
			return R.data(chatGroupBuildingService.createWxPlanFromRobot(robotCreatePlanVO, robotCreatePlanDTO));
		}else if(new Integer(MaintainPlanContentEnum.MAINTAIN_KR.getId()).equals(robotCreatePlanDTO.getType())){
			return R.data(chatGroupBuildingService.createKrPlanFromRobot(robotCreatePlanVO, robotCreatePlanDTO));
		}
		return R.data(null);
	}

	/**
	 * 根据工单id查询工单详情
	 */
	@GetMapping("/work/plan/detail")
	@ApiOperation("根据工单id查询工单详情")
	@ApiOperationSupport(order = 3)
	public R<RobotPlanDetailVO> getWorkPlanDetail(String ticketId){
		if(Func.isBlank(ticketId)){
			throw new ServiceException("工单id不能为空");
		}
		Long planId = Long.parseLong(ticketId);
		return R.data(chatGroupBuildingService.getWorkPlanDetailByPlanId(planId));
	}

	/**
	 * 根据梯号查询工单列表
	 */
	@GetMapping("/work/plan/list/by/elevator")
	@ApiOperation("根据梯号查询工单列表")
	@ApiOperationSupport(order = 4)
	public R<List<RobotPlanDetailVO>> getWorkPlanListByElevator(
		String roomId, String elevatorAddress,
		Date startDate,
		Date endDate){
		//  根据 roomId 查询对应的电梯id
		ElevatorDetail elevatorDetail = elevatorDetailService.getByElevatorAddress(roomId,elevatorAddress);
		if(Func.isNull(elevatorDetail)){
			throw new ServiceException("无法定位到"+elevatorAddress+"对应的电梯信息");
		}
		Long elevatorId = elevatorDetail.getElevatorId();
		List<RobotPlanDetailVO> result = chatGroupBuildingService.getWorkPlanListByElevator(elevatorId, startDate, endDate);
		return R.data(result);
	}

}
